/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.bomt.domain.vo;

import com.comac.ins.bomt.common.enums.ModelStatusChangeStatisticTypeEnum;
import com.comac.ins.common.core.exception.BizException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Summary status strength change vo.
 *
 * @author zhanghongyi
 * @date 2024 /8/5 14:38
 * @description SummaryStatusStrengthChangeVo
 */
@Data
public class SummaryStatusStrengthChangeVo {
    /**
     * 变更类型统计，key为changeType，value为对应类型的变更列表
     */
    private Map<String, Integer> changeCountMap = new HashMap<>();

    /**
     * 版本列表
     */
    private List<String> versions = new ArrayList<>();

    /**
     * 变更明细描述，key为changeType，value为对应类型变更描述信息字符串
     */
    private Map<String, String> changeDetailStringMap = new HashMap<>();

    /**
     * 变更分类明细List
     */
    @JsonIgnore
    @JsonProperty
    private List<DetailStatusStrengthChangeVo> changeDetailList = new ArrayList<>();

    /**
     * 导出摘要日志
     *
     * @param outputStream the output stream
     */
    public void exportSummaryLog(OutputStream outputStream) {
        StringBuilder summaryLogSb = new StringBuilder();
        summaryLogSb.append(MessageFormat.format("========================={0,number,#}个零组件删除：=========================\n", changeCountMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.DELETE.getType(), 0)));
        summaryLogSb.append(MessageFormat.format("{0}\n", changeDetailStringMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.DELETE.getType(), "")));
        summaryLogSb.append(MessageFormat.format("{0,number,#}个零组件新增：\n", changeCountMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.ADD.getType(), 0)));
        summaryLogSb.append(MessageFormat.format("{0}\n", changeDetailStringMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.ADD.getType(), "")));
        summaryLogSb.append(MessageFormat.format("{0,number,#}个零组件建模升版\n", changeCountMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.MODEL_UPGRADE.getType(), 0)));
        summaryLogSb.append(MessageFormat.format("{0}\n", changeDetailStringMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.MODEL_UPGRADE.getType(), "")));
        summaryLogSb.append(MessageFormat.format("{0,number,#}个零组件建模升号\n", changeCountMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.NUMBER.getType(), 0)));
        summaryLogSb.append(MessageFormat.format("{0}\n", changeDetailStringMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.NUMBER.getType(), "")));
        summaryLogSb.append(MessageFormat.format("{0,number,#}个零组件简单复制升版\n", changeCountMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.COPY_UPGRADE.getType(), 0)));
        summaryLogSb.append(MessageFormat.format("{0}\n", changeDetailStringMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.COPY_UPGRADE.getType(), "")));
        summaryLogSb.append(MessageFormat.format("{0,number,#}个零组件不变\n", changeCountMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.SAME.getType(), 0)));
        summaryLogSb.append(MessageFormat.format("{0}\n", changeDetailStringMap.getOrDefault(ModelStatusChangeStatisticTypeEnum.SAME.getType(), "")));
        try {
            outputStream.write(summaryLogSb.toString().getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new BizException(MessageFormat.format("output summary fail:{0}", e.getMessage()));
        }
    }
}
